<!--广告管理-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="新闻名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入新闻名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="发布时间" prop="state">
        <date-range-picker v-model="queryParams" value-format="timestamp" content-style="width: 240px"
                           startPickerName="startTime" endPickerName="endTime"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="newList">
      <el-table-column
        label="新闻名称"
        align="center"
        prop="title"
        width="140"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="状态" align="center" prop="state" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="statusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="富文本"
        align="center"
        prop="richText"
        width="120"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleRichText(scope.row)"
            v-hasPermi="['car:advertisement:edit']"
          >查看</el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="内容"
        align="center"
        prop="content"
        width="840"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="发布时间"
        align="center"
        prop="createTime"
        width="140"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">{{ parseTime(scope.row.createlongtime, '{m}/{d}/{y}') }}</template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpload(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="showDetails(scope.row)"
            >查看</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情 -->
    <el-dialog title="新闻详情" :visible.sync="open" width="800px" append-to-body>
      <el-card>
        <el-row class="details-content-row">
          <el-col :span="3">标题名称</el-col>
          <el-col :span="16">{{ currentObj.title }}</el-col>
        </el-row>
        <el-row class="details-content-row">
          <el-col :span="3">内容名称</el-col>
          <el-col :span="16">{{ currentObj.content }}</el-col>
        </el-row>
        <el-row class="details-content-row">
          <el-col :span="3">状态</el-col>
          <el-col :span="16">
            <dict-tag :options="dict.type.advertision_status" :value="currentObj.state"></dict-tag>
          </el-col>
        </el-row>
        <el-row class="details-content-row">
          <el-col :span="3">创建时间</el-col>
          <el-col :span="16">{{ parseTime(currentObj.createlongtime, '{m}/{d}/{y}') }}</el-col>
        </el-row>
      </el-card>
    </el-dialog>

    <!-- 添加或修改用户列表对话框 -->
    <el-dialog :title="title" :visible.sync="open1" width="1000px" append-to-body @close="close">
      <el-form ref="formOpens" :model="form" :rules="rules">
        <el-form-item label="标题" prop="title">
          <el-input class="modal-input" v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <!--        <el-form-item label="内容" prop="content">
          <el-input class="modal-input" type="textarea" v-model="form.content" placeholder="请输入内容"/>
        </el-form-item>-->
        <el-form-item label="内容" prop="content">
          <editor v-model="form.content" :min-height="220" />
        </el-form-item>
        <el-form-item label="发布状态" prop="state">
          <el-switch
            v-model="form.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
    <!-- 查看富文本 -->
    <el-dialog title="广告富文本" :visible.sync="openRichText" width="100%" append-to-body>
      <div v-html="richText" style="overflow: auto;" class="imgBox"></div>
    </el-dialog>
  </div>
</template>

<script>
import Template from '@/views/setting/template/index.vue'
import DateRangePicker from '@/components/DateRangePicker/index.vue'
import OssImagePreview from '@/components/ImagePreview/oss-image-preview.vue'
import {
  platformNews,
  platformNewsAdd,
  platformNewsDel,
  platformNewsInfo,
  platformNewsUpload,
  updateState
} from '@/api/content/newList'
import { addList, updateList } from '@/api/user/list'

export default {
  name: 'newList',
  components: { OssImagePreview, DateRangePicker, Template },
  dicts: ['position_options', 'advertision_status'],
  data () {
    return {
      form: {
        title: '',
        content: '',
        state: 0,
      },
      positionOptions: [{ value: 0, label: '平台公告' }, { value: 1, label: '推送消息' }],
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 平台广告管理表格数据
      newList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null
      },

      currentObj: {},
      showSearch: true,
      // 表单校验
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ],
      },
      richText: null,
      openRichText: false,
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    close () {
      this.$refs['formOpens'].resetFields()
      this.form.content = null
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.form = {}
      this.open1 = true
      this.title = '添加新闻列表'
    },
    handleUpload (row) {
      this.open1 = true
      this.$nextTick(() => {
        this.form = { ...row }
      })
      this.title = '修改新闻列表'
    },
    /** 查询平台广告管理列表 */
    getList () {
      this.loading = true
      platformNews(this.queryParams).then(response => {
        this.newList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs['formOpens'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            platformNewsUpload(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open1 = false
              this.getList()
            })
          } else {
            platformNewsAdd(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open1 = false
              this.getList()
            })
          }
        }
      })
    },
    closeOpen1 () {

    },
    // 取消按钮
    cancel () {
      this.open = false
      this.open1 = false
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm('queryForm')
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        name: null
      }
      this.handleQuery()
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除该条新闻？').then(function () {
        return platformNewsDel(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    // 查看富文本
    handleRichText (row) {
      const regex = new RegExp('<img', 'gi')
      row.content = row.content.replace(regex, `<img style="max-width: 100%; height: auto"`)
      this.richText = row.content

      this.openRichText = true
    },
    /** 修改状态 */
    statusChange (row) {
      updateState(row).then(response => {
        this.$modal.msgSuccess('修改成功')
        this.getList()
      })
    },
    /** 修改状态 */
    showDetails (row) {
      platformNewsInfo(row.id).then(response => {
        this.currentObj = response.data
        this.open = true
      })
    }
  }
}
</script>
<style scope lang="scss">
.details-content-row {
  padding: 10px 0;
}
</style>
